//
//  TipView.m
//  MyMovie
//
//  Created by Weiwei on 16/3/5.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "TipView.h"

@interface TipView (){
    
    NSArray* _dataList;//collectionView的数据源
    
    NSArray* _array1;//第一个页面数据
    NSArray* _array2;
    NSArray* _array3;
    
    
  
    
}

@end


@implementation TipView

//XIB和awakefromNIb
#pragma -mark 第一步
//xib加载view会走awakeFormNib
-(void)awakeFromNib{
    
    [super awakeFromNib];
    
    
    [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_top_movie_background_cover"]]];
    
#pragma -mark 4增加一个数据方法
    [self createData];
    
    
    
    
#pragma -mark 1添加一个collectionView
//1.初始化collectionView注册单元格
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"tip_cell"];
    
    
#pragma -mark 2创建一个segment
    SegmentView* _segmentView = [[SegmentView alloc]initWithFrame:CGRectMake(0, 0, self.width -50, 50) withTitleArray:@[@"特色",@"商圈",@"地区",@"地铁"]];
    
    
    __weak TipView* weakSelf = self;
    [_segmentView setSegmentBlock:^(NSInteger index) {
        
        __strong TipView* strongSelf = weakSelf;
        //点击segment切换数据
#pragma -mark5 创建一个方法，根据index改变datalist
        
        [strongSelf changeData:index];
        
        
    }];
    
    [self addSubview:_segmentView];
    
#pragma -mark 3添加一个button
    UIButton* _closeButton = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 50, 5, 40, 40)];
    
    [_closeButton setBackgroundImage:[UIImage imageNamed:@"pic_ico_wrong"] forState:UIControlStateNormal];
    
    [_closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_closeButton];
    
}

-(void)closeAction:(UIButton*)button{
    
   
    
    [UIView animateWithDuration:.3 animations:^{
         self.transform = CGAffineTransformIdentity;
    }];
    
    
    
}
//创建数据
-(void)createData{
    
    
    //给三个页面分别创建单独的数据array，然后默认将第一个数据数组给dataList复制，那么上来默认显示的就是第一个属猪的内容
    
    //特色
    _array1 = @[@"全部",@"IMAX厅",@"中国巨幕",@"4K放映厅",@"3D厅",@"杜比全景声厅",@"情侣座",@"停车场",@"Wi-Fi"];
    //商圈
    _array2 = @[@"全部",@"美食广场",@"恒隆广场"];
    
    //地区
    _array3 = @[@"全部",@"历下区",@"市中区",@"槐荫区",@"商河区",@"历城区",@"长清区"];
    
    
    //默认实现的是第一个页面数据
    _dataList = [NSArray arrayWithArray:_array1];
    
}

#pragma -mark 实现代理方法 
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _dataList.count;
    
}


-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tip_cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    
    
    
    for (UIView* view in cell.contentView.subviews) {
        
        if ([view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
        
        
    }
    
    
    //创建一个lable
    UILabel* lable = [[UILabel alloc]initWithFrame:cell.contentView.bounds];
    lable.font = [UIFont systemFontOfSize:20];
    lable.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:lable];
    
    
    lable.text = _dataList[indexPath.row];
    
    
    
    return cell;
    
    
    
    
}

-(void)changeData:(NSInteger)index{
    
    switch (index) {
        case 0:
            _dataList = [NSArray arrayWithArray:_array1];
            break;
        case 1:
            _dataList = [NSArray arrayWithArray:_array2];
            break;
        case 2:
            _dataList = [NSArray arrayWithArray:_array3];
            break;
        
        default:
            
            _dataList = @[];
            break;
    }
    
    //刷新数据
    [_collectionView reloadData];
    
    
    
    
    
}



@end
